package readWrite;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Vector;

import Jama.Matrix;

public class readWriteHelper {
	
	public static void writeFileObject(String filename, Matrix matrixA){
		BufferedWriter bufferedWriter = null;
        double [][] matrixAux=matrixA.getArray();
    	
  
        
        try {

            //Construct the BufferedWriter object
            bufferedWriter = new BufferedWriter(new FileWriter(filename));
            
            for(int i=0; i<matrixAux.length; i++ ){
            	 for(int j=0; j<matrixAux.length; j++ ){
            		 bufferedWriter.write(String.valueOf(matrixAux[i][j]) + ",");
 	            }
            	bufferedWriter.newLine();
            }
            //Start writing to the output stream
            
            
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedWriter
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
	}
	
	public static synchronized void writeFile(String filename, Matrix matrixA){
		   BufferedWriter bufferedWriter = null;
	        double [][] matrixAux=matrixA.getArray();
	    	
	      	
	     
	        
	        try {

	            //Construct the BufferedWriter object
	            bufferedWriter = new BufferedWriter(new FileWriter(filename));
	            
	     //       o_out.writeObject(matrixA);
	            for(int i=0; i<matrixAux.length; i++ ){
	            	 for(int j=0; j<matrixAux.length; j++ ){
	            		 bufferedWriter.write(String.valueOf(matrixAux[i][j]) + ",");
	 	            }
	            	bufferedWriter.newLine();
	            }
	       //     Start writing to the output stream
	            
	            
	        } catch (FileNotFoundException ex) {
	            ex.printStackTrace();
	        } catch (IOException ex) {
	            ex.printStackTrace();
	        } finally {
	            //Close the BufferedWriter
	            try {
	                if (bufferedWriter != null) {
	                    bufferedWriter.flush();
	                    bufferedWriter.close();
	                }
	            } catch (IOException ex) {
	                ex.printStackTrace();
	            }
	        }
		
	}
	
	public static String readFile(String file){
		BufferedReader br = null;
		String stringMatrix="";
		try {
 
			String sCurrentLine;
 
			br = new BufferedReader(new FileReader(file));
 
			while ((sCurrentLine = br.readLine()) != null) {
				stringMatrix=stringMatrix.concat(sCurrentLine + "\n");
			}
	
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return stringMatrix;
	}
	
	public static Matrix readFile2Object(String file){
		BufferedReader br = null;
		Vector rowsValues = new Vector ();
		try {
 
			String sCurrentLine;
 
			br = new BufferedReader(new FileReader(file));
 
			while ((sCurrentLine = br.readLine()) != null) {
				String [] values = sCurrentLine.split(",");
				rowsValues.add(values);
			}
			
			double [][] newMatrix = new double [rowsValues.size()][];
			for(int i=0; i<rowsValues.size(); i++ ){

				newMatrix[i]=stringToDouble((String[]) rowsValues.get(i)) ;
			}
			
			return new Matrix(newMatrix);
	
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public static synchronized Matrix readFile2(String file){
		BufferedReader br = null;
		Vector rowsValues = new Vector ();
		try {
 
			String sCurrentLine;
 
			br = new BufferedReader(new FileReader(file));
 
			while ((sCurrentLine = br.readLine()) != null) {
				String [] values = sCurrentLine.split(",");
				rowsValues.add(values);
			}
			
			double [][] newMatrix = new double [rowsValues.size()][];
			for(int i=0; i<rowsValues.size(); i++ ){

				newMatrix[i]=stringToDouble((String[]) rowsValues.get(i)) ;
			}
			
			return new Matrix(newMatrix);
	
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}
	
	public static double [] stringToDouble(String [] stringArray){
		
		double [] doubleArray=new double[stringArray.length];
		
		for(int i=0; i<stringArray.length; i++ ){
			doubleArray[i]=Double.parseDouble(stringArray[i]);
		}
		return doubleArray;
	}
	
	public static void deleteFile(String name){
		
		File file= new File(name);
		if(file.exists())file.delete();
	}
	
	
}
